import LevelPre_15 from "./LevelPre_15";

const { ccclass, property } = cc._decorator;

enum OPTION_TYPE {
    LEFT = 1, RIGHT
}
let OPTION_LEFT_NAME = ""
let OPTION_RIGHT_NAME = ""
let OptionAttachment = [];

@ccclass
export default abstract class Level_15_PickDress extends cc.Component {
    @property(sp.Skeleton)
    optionSpine: sp.Skeleton = null;
    @property(sp.Skeleton)
    sitSpine: sp.Skeleton = null;
    @property(sp.Skeleton)
    standSpine: sp.Skeleton = null;
    @property(dragonBones.ArmatureDisplay)
    smoke: dragonBones.ArmatureDisplay = null;
    @property(cc.Integer)
    optionNum: number = 8;

    level15: LevelPre_15 = null
    curNum: number = 0;
    leftBtn: cc.Node;
    rightBtn: cc.Node;
    optionType: OPTION_TYPE = OPTION_TYPE.LEFT;

    onLoad() {
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
        this.leftBtn = cc.find("bg1/left", this.node);
        this.rightBtn = cc.find("bg1/right", this.node);

        this.smoke.addEventListener(dragonBones.EventObject.COMPLETE, this.smokeEnd, this);
    }
    smokeEnd() {
        this.smoke.node.active = false
    }
    initData(leftName: string, rightName: string, attachmentList: unknown[]) {
        OPTION_LEFT_NAME = leftName;
        OPTION_RIGHT_NAME = rightName;
        OptionAttachment = attachmentList;
    }
    start() {
        this.initGirl();
        this.updateOptionLeft();
        this.updateOptionRight();
    }
    abstract initGirl(): void;
    updateOptionLeft() {
        this.optionSpine.findBone(OPTION_LEFT_NAME).active = true;
        this.optionSpine.setAttachment(OPTION_LEFT_NAME, OptionAttachment[this.curNum].left);
    }
    updateOptionRight() {
        this.optionSpine.findBone(OPTION_RIGHT_NAME).active = true;
        this.optionSpine.setAttachment(OPTION_RIGHT_NAME, OptionAttachment[this.curNum].right);
    }

    clickBtnLeft() {
        let talkData = {
            str: "我选左",
            audio: {
                resUrl: "left",
                value: 1
            }
        }
        this.level15.sayNodeTween(talkData);

        this.optionType = OPTION_TYPE.LEFT;
        this.optionSpine.findBone(OPTION_LEFT_NAME).active = false;
        this.clickBtn();
    }
    clickBtnRight() {
        let talkData = {
            str: "我选右",
            audio: {
                resUrl: "right",
                value: 1
            }
        }
        this.level15.sayNodeTween(talkData);

        this.optionType = OPTION_TYPE.RIGHT;
        this.optionSpine.findBone(OPTION_RIGHT_NAME).active = false;
        this.clickBtn();
    }
    clickBtn() {
        this.curNum++;
        this.leftBtn.active = false;
        this.rightBtn.active = false;
        this.smoke.node.active = true;
        this.smoke.playAnimation("Sprite", 1);
        cc.tween(this.node).delay(0.3).call(() => this.switchDress()).start();
    }
    async switchDress() {
        this.updateGirl();
        this.talkEnd()
        // let audio = Audio[this.curNum - 1];
        // let talkData = {
        //     str: this.optionType === OPTION_TYPE.LEFT ? audio.left : audio.right,
        //     audio: {
        //         resUrl: this.curNum + "" + this.optionType,
        //         value: 1
        //     }
        // }
        // let talkTime = await this.level15.sayNodeTween(talkData);
        // cc.tween(this.node).delay(talkTime).call(() => this.talkEnd()).start();
    }

    talkEnd() {
        this.level15.addHaveStates([this.curNum + "" + this.optionType]);

        if (this.curNum < this.optionNum) {
            this.leftBtn.active = true;
            this.rightBtn.active = true;
            this.updateOptionLeft();
            this.updateOptionRight();
        } else {
            this.level15.updateIsWin();
        }
    }
    abstract updateGirl(): void;
}